/**
 * @file pthrds.h
 * @brief Macros of the pthreads functions.
 * @date Dec-2011
 * @author Leonardo Lino 2101101
 * @author Joaquim Ferreira 2101104
 * @version 1.0
 */

#ifndef PTHRDS_H
#define PTHRDS_H

/** @brief This global integer variable (G_continue) will be used to treat the signal breakpoint to free all the resources.*/
extern int G_continue;


extern HASHTABLE_T* finalResults;


/** @brief This structure will all the variables needed to deal with pthreads.*/
typedef struct 
{
    /**< @brief **buffer: The buffer will countain the pointers to the strings of the path_file.*/
    char **buffer;
    /**< @brief index_read:Index of the buffer to read.*/
    int index_read;
    /**< @brief index_write: Index of the buffer to write.*/
    int index_write;
    /**< @brief total: The total number of elements in the bufffer.*/
    int total;
    /**< @brief max: Buffer capacity.*/
    int max;
    /**< @brief stop: This is a flag that will set the consumers threads to terminate.*/
    int stop;
    /**< @brief type: Controls where this structure is being called from. Can be either freq-folder or topp-errors.*/
    int type;
    /**< @brief mutex: Holds dicitonay. Used in top-errors*/
    HASHTABLE_T* dictHolder;
    
    /**< @brief mutex: The mutex.*/
    pthread_mutex_t mutex;
    /**< @brief cond: The condition variable.*/
    pthread_cond_t cond;
} PARAM_T;

/**
 * @brief Function that manage the process of the buffer, the consumer threads, join the local hashtable and show the word histogram.
 * @fn void m_thread(char *folder, int max_thr)
 * @param folder A pointer to the folder string.
 * @param max_thr An integer that countains the number of the threads.
 * @param type An integer that countains type. Used to define where this function is being called from. Can be called from freq-folder or top-errors
 * @return Nothing to return.
 */
void m_thread (char *folder, int max_thr, int type, char *dictionary);

/**
 * @brief Function of the producer processing a folder and sub-folders.
 * @fn process_folder(PARAM_T *param, char *folder, int *counter)
 * @param param A pointer to the type structure PARAM_T.
 * @param folder A pointer to the folder string.
 * @param counter A pointer to a integer variable that countains the number of text files.
 * @return Nothing to return.
 */
void process_folder (PARAM_T *param, char *folder, int *counter);

/**
 * @brief Function of the producer putting an element to the buffer.
 * @fn process_folder(PARAM_T *param, char *folder, int *counter)
 * @param param A pointer to the type structure PARAM_T.
 * @param path_file A pointer to the path_file string.
 * @return Nothing to return.
 */
void *put_buffer (PARAM_T *p, char *path_file);

/**
 * @brief Function of the consumer(s) thread(s).
 * @fn void *consumer(void *arg)
 * @param arg A generic pointer.
 * @return Return a generic pointer.
 */
void *consumer (void *arg);

/**
 * @brief Function that add the key and the value to the main hashtable or update 
 * an existent value from a given local hashtables and show the word histogram.
 * @fn void join_show_hasht(HASHTABLE_T **loc_hasht, int num_hasht)
 * @param loc_hasht A pointers array to the local hashtables.
 * @param num_hasht A integer variable that countains the number of local hashtables.
 * @return Nothing to return.
 */
void join_show_hasht (HASHTABLE_T **loc_hasht, int num_hasht);

#endif /* PTHRDS_H */
